<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="箱子编号" prop="boxCode">
        <el-input
          v-model="queryParams.boxCode"
          placeholder="请输入箱子编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="装箱时间" prop="boxTime">
        <el-date-picker clearable
          v-model="queryParams.boxTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择装箱时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="箱子容量" prop="boxCapacity">
        <el-input
          v-model="queryParams.boxCapacity"
          placeholder="请输入箱子容量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="箱子状态" prop="boxState">
        <el-select v-model="queryParams.boxState" placeholder="请选择箱子状态" clearable>
          <el-option
            v-for="dict in dict.type.t_box_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出库状态" prop="ckState">
        <el-select v-model="queryParams.ckState" placeholder="请选择出库状态" clearable>
          <el-option
            v-for="dict in dict.type.t_ck_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="出库时间" prop="ckTime">
        <el-date-picker clearable
          v-model="queryParams.ckTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出库时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="是否封箱" prop="extend1">
        <el-select v-model="queryParams.extend1" placeholder="请选择" clearable>
          <el-option label="是" value="1"/>
          <el-option label="否" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item label="装箱人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入装箱人名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddKs"
          v-hasPermi="['system:box:add']"
        >快速新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:box:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:box:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:box:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:box:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-success"
          :disabled="multiple"
          @click="handleFx(1)"
        >批量封箱</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-right"
          :disabled="multiple"
          @click="handleCk('two')"
        >批量出库</el-button>
      </el-col>
      
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="boxList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="箱号" align="center" prop="boxCode" width="150" />
      <!-- <el-table-column label="容量" align="center" prop="boxCapacity" /> -->
      <!-- <el-table-column label="状态" align="center" prop="boxState">
        <template slot-scope="scope">
         <span v-if="scope.row.deviceCount<scope.row.boxCapacity">未装满</span>
         <span v-if="scope.row.deviceCount>= scope.row.boxCapacity">已装满</span>
        </template>
      </el-table-column> -->
      <el-table-column label="设备数量" align="center" prop="deviceCount" width="90">
        <template slot-scope="scope">
          <span style="color: #409EFF;font-weight: bold;cursor: pointer;" @click="findDevice(scope.row)">{{scope.row.deviceCount}} / <span style="color:gray">{{scope.row.boxCapacity}}</span></span>
        </template>
      </el-table-column>  
      <el-table-column label="装箱人" align="center" prop="createName" width="90" />
      <el-table-column label="时间" align="center" prop="boxTime" width="120">
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="出库状态" align="center" prop="ckState" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.ckState == 0" style="color: orange;font-weight: bold;">未出库</span>
          <span @click="showCkInfo(scope.row)" v-if="scope.row.ckState == 1" style="color: #409EFF;font-weight: bold;cursor: pointer;">已出库</span>
        </template>
      </el-table-column>
      <el-table-column label="出库时间" align="center" prop="ckTime" width="120"></el-table-column>
      <el-table-column label="客户名称" align="left" header-align="center" prop="customerName" width="220" show-overflow-tooltip></el-table-column>
      <el-table-column label="客户地址" align="left" header-align="center" prop="customerAddress"  show-overflow-tooltip></el-table-column>
      <el-table-column label="是否封箱" align="center" prop="extend1" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.extend1 == 0" style="color: orange;font-weight: bold;">未封箱</span>
          <span v-if="scope.row.extend1 == 1" style="color: green;font-weight: bold;">已封箱</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" header-align="center" class-name="small-padding fixed-width" width="290">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-printer"
            @click="showPrint(scope.row)"
            v-hasPermi="['system:box:edit']"
          >打印</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:box:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:box:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-bottom"
            @click="exportPdf(scope.row)"
          >导出PDF</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-right"
            @click="handleChuku(scope.row,'one')"
            v-if="scope.row.ckState == 0"
            v-hasPermi="['system:box:edit']"
          >出库</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="设备" :visible.sync="openDevice" width="1500px" append-to-body>
      <listDeviceData ref="childDevice"></listDeviceData>
    </el-dialog>

    <!-- 添加或修改装箱管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="装箱时间" prop="boxTime">
          <el-date-picker clearable
            v-model="form.boxTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择装箱时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="是否封箱" prop="extend1" v-if="title=='修改箱子'">
          <el-select v-model="form.extend1" placeholder="请选择">
            <el-option label="未封箱" value="0"></el-option>
            <el-option label="已封箱" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出库状态" prop="ckState" v-if="title=='修改箱子'">
          <el-select v-model="form.ckState" placeholder="请选择出库状态">
            <el-option
              v-for="dict in dict.type.t_ck_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="出库时间" prop="ckTime">
          <el-date-picker clearable
            v-model="form.ckTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出库时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="创建人" prop="createName">
          <el-input v-model="form.createName" placeholder="请输入创建人" />
        </el-form-item> -->
        <el-form-item label="箱子容量" prop="boxCapacity">
          <el-input-number v-model="form.boxCapacity" :min="1"  label="箱子容量"></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 出库信息填写 -->
    <el-dialog title="填写出库信息" :visible.sync="ckInfoOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="地址" prop="customerAddress">
          <el-input v-model="form.customerAddress"  placeholder="请输入客户地址" />
        </el-form-item>
        <el-form-item label="联系方式" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入客户联系方式" />
        </el-form-item>
        <el-form-item label="其他信息" prop="customerInfo">
          <el-input v-model="form.customerInfo" placeholder="请输入其他信息" />
        </el-form-item>
        <el-form-item label="客户备注" prop="customerRemark">
          <el-input v-model="form.customerRemark" type="textarea" :rows="3" placeholder="请输入客户备注" />
        </el-form-item>
      </el-form>  
      <div slot="footer" class="dialog-footer">
        <el-button v-if="saveCk=='one'" type="primary" @click="chuKuApi">确 定</el-button>
        <el-button v-if="saveCk=='two'" type="primary" @click="handleCkApi">确 定</el-button>
        <el-button @click="ckInfoOpen=false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 出库信息查看 -->
    <el-dialog title="查看出库信息" :visible.sync="ckInfoOpenView" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" :readonly="true" />
        </el-form-item>
        <el-form-item label="地址" prop="customerAddress">
          <el-input v-model="form.customerAddress"  placeholder="请输入客户地址" :readonly="true" />
        </el-form-item>
        <el-form-item label="联系方式" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入客户联系方式" :readonly="true" />
        </el-form-item>
        <el-form-item label="其他信息" prop="customerInfo">
          <el-input v-model="form.customerInfo" placeholder="请输入其他信息" :readonly="true" />
        </el-form-item>
        <el-form-item label="客户备注" prop="customerRemark">
          <el-input v-model="form.customerRemark" type="textarea" :rows="3" placeholder="请输入客户备注" disabled/>
        </el-form-item>
      </el-form>  
      <div slot="footer" class="dialog-footer">
        <el-button @click="ckInfoOpenView=false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加自定义打印组件 -->
    <el-dialog title="打印预览" :visible.sync="openPrint" width="920px" append-to-body>
      <div id="printConten">
        <div>
          <span style="font-size: 18px;font-weight: bold;">编号： {{printBoxData.boxCode}}</span>
          <span style="font-size: 18px;font-weight: bold;margin-left:50px;">数量： {{printBoxData.deviceCount}} / {{printBoxData.boxCapacity}}</span>
          <span style="font-size: 18px;font-weight: bold;margin-left:50px;">装箱人： {{printBoxData.createName}}</span>
          <span style="font-size: 18px;font-weight: bold;margin-left:50px;">时间： {{printBoxData.boxTime}}</span>
          <!-- <el-table :data="printBoxList" border style="width: 100%">
            <el-table-column prop="boxCode" label="编号" width="205" align="center">
            </el-table-column>
            <el-table-column prop="boxCode" label="数量" width="205" align="center">
              <template slot-scope="scope">
                {{scope.row.deviceCount}} / {{scope.row.boxCapacity}}
              </template>
            </el-table-column>
            <el-table-column prop="createName" label="装箱人" width="205" align="center">
            </el-table-column>
            <el-table-column prop="boxTime" label="时间" width="205" align="center">
            </el-table-column>
          </el-table>  -->
        </div>
        <div style="display: flex;margin-top:20px;">
          <div v-if="boxDeviceList1.length>0">
            <el-table :data="boxDeviceList1"  border style="width: 100%" :show-header="false">
              <el-table-column
                prop="deviceCode"
                label="设备编号"
                width="205" align="center"
               >
              </el-table-column>
            </el-table>
          </div>
          <div v-if="boxDeviceList2.length>0">
            <el-table :data="boxDeviceList2"  border style="width: 100%" :show-header="false">
              <el-table-column
                prop="deviceCode"
                label="设备编号"
                width="205" align="center"
                >
              </el-table-column>
            </el-table>
          </div>
          <div v-if="boxDeviceList3.length>0">
            <el-table :data="boxDeviceList3"  border style="width: 100%" :show-header="false">
              <el-table-column
                prop="deviceCode"
                label="设备编号"
                width="205" align="center"
                >
              </el-table-column>
            </el-table>
          </div>
          <div v-if="boxDeviceList4.length>0">
            <el-table :data="boxDeviceList4"  border style="width: 100%" :show-header="false">
              <el-table-column
                prop="deviceCode"
                label="设备编号"
                width="205" align="center"
                >
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handlePrinter">打 印</el-button>
        <el-button @click="openPrint=false">取 消</el-button>
      </div>  
    </el-dialog> 
  </div>
</template>

<script>
import { listBox, getBox, delBox, addBox, updateBox, downloadBox,fxBox,ckBox } from "@/api/box/box";
import listDeviceData from "@/views/device/index_child";
import { listDevice } from "@/api/box/device";
import printJS from 'print-js';
import axios from 'axios'
export default {
  name: "Box",
  dicts: ['t_box_state', 't_ck_state'],
  components:{listDeviceData},
  data() {
    return {
      saveCk:'',
      selectBoxs:"",
      boxDeviceList1: [],
      boxDeviceList2: [],
      boxDeviceList3: [],
      boxDeviceList4: [],
      ckInfoOpenView: false,
      ckInfoOpen: false,
      openDevice: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 装箱管理表格数据
      boxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boxCode: null,
        boxTime: null,
        boxCapacity: null,
        boxState: null,
        ckState: null,
        ckTime: null,
        createId: null,
        createName: null,
        deptId: null,
        extend1: null,
        extend2: null,
        extend3: null,
        extend4: null,
        extend5: null,
        extend6: null,
        customerAddress: null,
        customerName: null,
        customerPhone: null,
        customerInfo: null,
        customerRemark: null
      },
      // 表单参数
      form: {
        
      },
      // 表单校验
      rules: {
        customerName: [
            { required: true, message: '请输入客户名称', trigger: 'blur' }
        ],
        customerAddress: [
            { required: true, message: '请输入客户地址', trigger: 'blur' }
        ],
        customerPhone: [
            { required: true, message: '请输入客户联系方式', trigger: 'blur' }
        ],
      },
      printBoxData: {},
      printBoxList:[],
      openPrint:false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    exportPdf(row) {
      this.download('system/box/export/pdf/'+row.boxCode, {
        ...this.queryParams
      }, row.boxCode+`.pdf`)
    },
    // 打印预览
    showPrint(row){
      this.boxDeviceList1 = []; this.boxDeviceList2 = []; this.boxDeviceList3 = []; this.boxDeviceList4 = [];  
      this.printBoxData = row
      this.printBoxList = []
      this.printBoxList.push(row)
      listDevice({boxCode:row.boxCode,pageNum: 1,pageSize: 1000}).then(response => {
        let allDeviceList = response.rows;
        for(let i=0;i<allDeviceList.length;i++){
          if(i<25){
            this.boxDeviceList1.push(allDeviceList[i])
          }else if(i>=25 && i<50){
            this.boxDeviceList2.push(allDeviceList[i])
          }else if(i>=50 && i<75){
            this.boxDeviceList3.push(allDeviceList[i])
          }else if(i>=75 && i<1000){
            this.boxDeviceList4.push(allDeviceList[i])
          }
        }
      });
      this.openPrint = true;
    },
    // 打印
    handlePrinter() {
      this.$nextTick(() => {
        printJS({
          printable: 'printConten',
          type: 'html',
          targetStyles: ['*']
        });
      });
    },
    findDevice(row){
      this.openDevice = true
      this.$nextTick(() => {
        this.$refs.childDevice.childDevice(row.boxCode);
      })
    },
    /** 查询装箱管理列表 */
    getList() {
      this.loading = true;
      listBox(this.queryParams).then(response => {
        this.boxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        boxCode: null,
        boxTime: null,
        boxCapacity: 100,
        boxState: null,
        ckState: null,
        ckTime: null,
        createId: null,
        createName: null,
        createTime: null,
        deptId: null,
        updateTime: null,
        remark: null,
        extend1: null,
        extend2: null,
        extend3: null,
        extend4: null,
        extend5: null,
        extend6: null,
        customerAddress: null,
        customerName: null,
        customerPhone: null,
        customerInfo: null,
        customerRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.boxCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 快速新增
    handleAddKs(){
      this.form.boxCapacity = 100
      addBox(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.getList();
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加箱子";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const boxCode = row.boxCode || this.ids
      getBox(boxCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改箱子";
      });
    },
    //查看出库详情信息
    showCkInfo(row){
      this.ckInfoOpenView = true;
      getBox(row.boxCode).then(response => {
        this.form = response.data;
      });
    },
    //出库按钮点击
    handleChuku(row,type){
      this.saveCk = type
      this.ckInfoOpen = true;
      getBox(row.boxCode).then(response => {
        this.form = response.data;
      });
    },
    //出库数据提交接口
    chuKuApi() {
      this.form.ckState = 1
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateBox(this.form).then(response => {
            this.getList();
            if(response.code == 200){
              this.$modal.msgSuccess(response.msg);
            }else{
              this.$modal.msgError(response.msg);
            }
            this.ckInfoOpen = false;
          });
        }
      });  
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.boxCode != null) {
            updateBox(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBox(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const boxCodes = row.boxCode || this.ids;
      this.$modal.confirm('是否确认删除装箱管理编号为"' + boxCodes + '"的数据项？').then(function() {
        return delBox(boxCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleFx(type){
      const boxCodes = this.ids;
      this.$modal.confirm('是否批量操作编号为"' + boxCodes + '"的数据项？').then(function() {
        return fxBox(boxCodes,type);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    handleCk(type){
      this.saveCk = type
      this.selectBoxs = this.ids;
      this.reset();
      this.ckInfoOpen = true;
    },
    handleCkApi(){
      ckBox(this.selectBoxs,this.form).then(response => {
          this.$modal.msgSuccess("操作成功");
          this.ckInfoOpen = false;
          this.getList();
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/box/export', {
        ...this.queryParams
      }, `box_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
